package org.flowable.pz.controller;

import org.flowable.pz.entity.dto.FlowableDefinitionDto;
import org.flowable.pz.entity.vo.FlowableXmlVo;
import org.flowable.pz.entity.dto.FlowableResultDto;
import org.flowable.pz.service.FlowableDefinition;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 流程部署相关
 * */
@RestController
@RequestMapping("/flowable")
public class FlowableDeployController {

    private final FlowableDefinition flowableDefinition;

    public FlowableDeployController(FlowableDefinition flowableDefinition){
        this.flowableDefinition = flowableDefinition;
    }

    /**
     * 通过文件部署
     * tag：这个文件貌似需要是后缀 .bpmn20.xml，比如 demo.bpmn20.xml
     * */
    @RequestMapping("/deploy/deployByFile")
    public FlowableResultDto<Void> deploy(@RequestParam("file") MultipartFile file) {
        flowableDefinition.deployment(file);
        return FlowableResultDto.success();
    }

    /**
     * 通过 xml 字符串部署
     * */
    @RequestMapping("/deploy/deployByXML")
    public FlowableResultDto<Void> deploy(@RequestBody FlowableXmlVo flowXmlVo){
        flowableDefinition.deployment(flowXmlVo);
        return FlowableResultDto.success();
    }

    /**
     * 流程定义列表
     * */
    @GetMapping("/definition/list")
    public FlowableResultDto<List<FlowableDefinitionDto>> list(@RequestParam(value = "name", required = false)String name,
                                                               @RequestParam(value = "type", required = false) String type){
        return FlowableResultDto.success(flowableDefinition.definitionList(name, type));
    }

    /**
     * 流程定义挂起/激活
     * */
    @PostMapping("/definition/suspend/{definitionId}")
    public FlowableResultDto<Void> suspend(@PathVariable("definitionId") String definitionId){
        flowableDefinition.suspend(definitionId);
        return FlowableResultDto.success();
    }
}
